<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { format, addDate } from '@/util'
const URL = 'https://haomiaozhichen.github.io/ebhs-review-data.json'
export default {
  name: 'App',
  data () {
    return {}
  },
  computed: {},
  created(){
    this.getWebData()
  },
  methods:{
    ...mapActions(['setStore']),
  /**生成 hasTaskDays有任务的所有日期的列表、dayTaskList有多个任务的日期列表
   * dayStr任务创建时间
   * taskId任务ID
   * hasTaskDays有任务的所有日期的列表，已去重
   * dayTaskList有多个任务的日期列表，统计某天有几个任务
   */
    getHasTaskDays(dayStr, taskItem, hasTaskDays, dayTaskList){
      let dayIdx = hasTaskDays.indexOf(dayStr)
      if(dayIdx === -1){//有任务的所有日期的列表里 没有，加
        hasTaskDays.push(format(new Date(dayStr),'YYYY-MM-DD'))
        dayTaskList.push({//加
          day:dayStr,
          taskList:[taskItem]
        })
      } else {//有任务的所有日期的列表里 有
        let taskList = dayTaskList[dayIdx].taskList //某天的任务列表 
        if(taskList.indexOf(taskItem) === -1){//没有，加
          taskList.push(taskItem)
          // dayTaskList[dayIdx].taskList = taskList
        }
      }
    },
    dealData(curveList,taskList){
      let taskDotList = [] // 某任务的复习日期点列表 dotList
      let hasTaskDays = [] // 有任务的所有日期的列表，已去重
      let dayTaskList = [] // 有多个任务的日期列表，统计某天有几个任务
      /**任务的复习状况列表
       * 列表项：{
       *    reviewTime:'2019/09/02', // 应该复习任务的时间，计算出来的
       *    hasReviewed:1,//是否已复习过了，1，复习过了
       * }
       */
      let reviewList = []
      //原始任务列表循环
      taskList.forEach((taskItem)=>{
        reviewList = []
        taskDotList = [] 
        curveList.forEach((curve)=>{
          if(curve.curveId === taskItem.curveId){
            taskDotList = curve.dotList
          }
        })
        let startDate = format(new Date(taskItem.createTime),'YYYY/MM/DD') //任务创建时间
        this.getHasTaskDays(startDate, taskItem, hasTaskDays, dayTaskList)
        taskDotList.forEach((dotDay,index)=>{//某任务的复习日期点列表 dotList
          let lastTime;
          if(index === 0){
            lastTime = startDate;//任务创建时间
          }else{
            lastTime = new Date(reviewList[index-1].reviewTime)//上次的复习时间
          }
          let day = addDate(lastTime, dotDay)
          let hasReviewed = taskItem.reviewRecord && taskItem.reviewRecord[index] ? 1 : 0;
          let today = new Date() // 今天
          if(day < today && !hasReviewed){//复习点还没复习 且 复习点早于今天
            day = today // 今天复习
          }
          let dayStr = format(day,'YYYY/MM/DD')
          reviewList.push({
            reviewTime:dayStr,
            hasReviewed,
            dot:dotDay // 复习间隔
          })
          this.getHasTaskDays(dayStr,taskItem, hasTaskDays, dayTaskList)
        })
        taskItem.reviewList = reviewList;
      })
      console.log('计算好了');
      this.setStore({
        curveList,
        hasTaskDays,
        dayTaskList, // 某天有哪些任务列表
        taskList // task 任务分布列表
      })
    },
    async getWebData(){
      this.$http.get(URL).then(({data}) => {
        //console.log('reviewTasks.json data:', data);
        data = {
          curveList:[{
            curveId:'star-0',
            desc:'有不重要的知识点 无星标知识点',
            dotList:[10,30,50,70,100,300,500,800]
          },{
            curveId:'star1-3',
            desc:'星标知识点1-3个',
            dotList:[5,10,30,50,70,100,300,500]
          },{
            curveId:'star4-6',
            desc:'星标知识点4-6个',
            dotList:[3,5,10,30,50,70,100,300,500]
          },{
            curveId:'star7-9',
            desc:'星标知识点7-9个',
            dotList:[2,3,5,10,30,50,70,100,300]
          },{
            curveId:'star10+',
            desc:'星标知识点大于10个',
            dotList:[1,2,4,7,15,30,60,105,225]
          }],
          taskList:[{
            taskId:'test1',
            taskName:'测试任务1',
            curveId:'star-0',
            createTime:'2019/06/04',
            reviewRecord:[1],//复习记录，已复习1
          },{
            taskId:'test2',
            taskName:'测试任务2',
            curveId:'star1-3',
            createTime:'2019/05/09',
            reviewRecord:[1,1,1],//复习记录，已复习1
          },{
            taskId:'test3',
            taskName:'测试任务3',
            curveId:'star4-6',
            createTime:'2019/04/09',
            reviewRecord:[1,1,1],//复习记录，已复习1
          },{
            taskId:'test4',
            taskName:'测试任务4',
            curveId:'star7-9',
            createTime:'2019/03/09',
            reviewRecord:[1,1,1],//复习记录，已复习1
          },{
            taskId:'test5',
            taskName:'测试任务5',
            curveId:'star10+',
            createTime:'2019/02/09',
            reviewRecord:[1,1,1],//复习记录，已复习1
          },{
            taskId:'task1',
            taskName:'阮一峰JS教程 1.语法 数据类型',
            curveId:'star-0',
            createTime:'2019/07/09'
          },{
            taskId:'task2',
            taskName:'阮一峰JS教程 2.1.数值',
            curveId:'star1-3',
            createTime:'2019/07/10'
          },{
            taskId:'task3',
            taskName:'阮一峰JS教程 2.3.数值转换',
            curveId:'star1-3',
            createTime:'2019/07/10'
          },{
            taskId:'task3',
            taskName:'阮一峰JS教程 2.4.字符串',
            curveId:'star1-3',
            createTime:'2019/07/10'
          },{
            taskId:'task3',
            taskName:'阮一峰JS教程 2.5.对象',
            curveId:'star1-3',
            createTime:'2019/07/10'
          }]
        }
        this.dealData(data.curveList,data.taskList);
      })
    }
  }
}
</script>

<style lang="less">
@import '~vux/src/styles/reset.less';
body {
  background-color: #fbf9fe;
}
</style>
